package com.telnetnz.ticketdesk.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.rockbb.common.simpleframe.action.ActionContext;
import com.rockbb.common.simpleframe.action.ActionNavigator;
import com.rockbb.common.simpleframe.config.SimpleConfig;
import com.rockbb.common.simpleframe.result.ActionResult;

public class BasicSessionPageAction extends BasicSessionAction
{

	private static Logger logger = Logger.getLogger(BasicSessionPageAction.class);

	private Map<String, Object> hidden_fields = new HashMap<String, Object>();
	private Map<String, Object> page = new HashMap<String, Object>();
	private Map<String, Object> css = new HashMap<String, Object>();
	private Map<String, Object> js = new HashMap<String, Object>();

	public void initialize(ActionContext context, HttpServletRequest request, HttpServletResponse response,
			Map<String, Object> root)
	{
		super.initialize(context, request, response, root);
		// 页面的主导航
		getRoot().put(ActionResult.MAP_KEY_NAVS, buildCurNavigations());
		// input类型下的隐藏表单字段, message和confirm类型下的消息标题, 内容, 额外表单, 隐藏字段等
		getRoot().put(ActionResult.MAP_KEY_HIDDEN_FIELDS, hidden_fields);
		// 当前页面数据
		getRoot().put(ActionResult.MAP_KEY_PAGE, page);
		// 当前页面上的css引用
		getRoot().put(ActionResult.MAP_KEY_CSS, css);
		// 当前页面上的js变量设定, js源文件引用设定
		getRoot().put(ActionResult.MAP_KEY_JS, js);
		// 处理页首导航栏高亮显示
		getRoot().put(ActionResult.MAP_KEY_CURRENT_NAV, getContext().getActionName());
	}

	/*
	 * hidden fields 用于在CONFIRM和MESSAGE类型的页面上传入数据
	 */
	protected void clear_hidden_fields()
	{
		hidden_fields.clear();
	}

	protected void put_hidden_field(String key, Object value)
	{
		hidden_fields.put(key, value);
	}

	protected void remove_hidden_field(String key)
	{
		hidden_fields.remove(key);
	}

	/*
	 * page map 是页面输出内容的主体
	 */
	protected void clear_page()
	{
		page.clear();
	}

	protected void put_page_key(String key, Object value)
	{
		page.put(key, value);
	}

	protected void remove_page_key(String key)
	{
		page.remove(key);
	}

	/*
	 * js map 用于定义与页面相关的js变量和js引用
	 */
	protected void clear_js()
	{
		js.clear();
	}

	protected void put_js_key(String key, Object value)
	{
		js.put(key, value);
	}

	protected void remove_js_key(String key)
	{
		js.remove(key);
	}

	/*
	 * css map 用于定义与页面相关的css引用
	 */
	protected void clear_css()
	{
		css.clear();
	}

	protected void put_css_key(String key, Object value)
	{
		css.put(key, value);
	}

	protected void remove_css_key(String key)
	{
		css.remove(key);
	}

	/**
	 * 为页首填充额外的meta元素, 注意会将先前调用的结果重置
	 * 
	 * @param extra_meta
	 */
	protected void extra_meta(List<String> extra_metas)
	{
		hidden_fields.put("extra_metas", extra_metas);
	}

	/**
	 * 为消息窗口填充元素
	 * 
	 * @param title
	 * @param content
	 */
	protected String message_box(String title, String content)
	{
		return message_box(title, content, null, 0);
	}

	/**
	 * 为消息窗口填充元素, 并在指定的延迟后, 跳转到相应的页面
	 * 
	 * @param title
	 * @param content
	 *            如果redirect不是null, 则会添加跳转的说明
	 * @param redirect
	 *            如果是null, 则不做跳转
	 * @param delay
	 *            延迟时间, 单位为秒
	 */
	protected String message_box(String title, String content, String redirect, int delay)
	{
		if (redirect != null && redirect.length() > 0)
		{
			List<String> metas = new ArrayList<String>();
			metas.add("<meta http-equiv=\"refresh\" content=\"" + delay + ";URL=" + redirect + "\">");
			hidden_fields.put("extra_metas", metas);
		}
		Map<String, String> message = new HashMap<String, String>();
		message.put("title", title);
		message.put("content", content);
		hidden_fields.put("message", message);

		return MESSAGE;
	}

	/**
	 * 为确认窗口填充元素, 包含隐藏的input集合
	 * 
	 * @param title
	 * @param content
	 * @param post_action
	 * @param extra_html
	 *            额外的html, 例如删除时的关系转移需要用到的下拉列表框等. 可以是null
	 * @param hidden_inputs
	 *            用于隐藏字段的输入, 例如删除时需要指定的id等
	 * @return
	 */
	protected String confirm_box(String title, String content, String post_action, String extra_html,
			Map<String, String> hidden_inputs)
	{
		Map<String, String> message = new HashMap<String, String>();
		message.put("title", title);
		message.put("content", content);
		hidden_fields.put("message", message);

		Map<String, String> form = new HashMap<String, String>();
		form.put("extra_html", extra_html);
		form.put("formaction", post_action);
		form.putAll(hidden_inputs);
		hidden_fields.put("form", form);
		return CONFIRM;
	}

	/**
	 * 向上追溯action context得到与导航栏中的action最近的父action
	 * 
	 * @param context
	 * @return
	 */
	private Map<String, ActionNavigator> buildCurNavigations()
	{
		Map<String, ActionNavigator> navigatorsMap = SimpleConfig.getCloneNavigators();
		List<ActionContext> actionPath = SimpleConfig.getActionPath(this.getContext().getActionName());
		Collection<ActionNavigator> navigators = navigatorsMap.values();
		for (ActionContext action: actionPath)
		{
			ActionNavigator current = navigatorsMap.get(action.getActionName());
			if (current != null)
			{
				current.setCurrent(1);
			}
		}
		return navigatorsMap;
	}

}
